<?php

namespace app\sisome\controller;

use think\Request;

class Fabu extends Common
{    //系统发布消息/文章等
    public function index()
    {
        // die('234');
        //return $this->fetch('hello',['name'=>'thinkphp']);
        //return view('hello',['name'=>'thinkphp']);
        // 方法    说明
        // fetch   渲染模板输出
        // display 渲染内容输出
        // assign  模板变量赋值
        // engine  初始化模板引擎
        return view();
    }

    /**
     * @name 公告列表
     * @author zay
     * @Date 2020/3/19 0019 16:16
     * @return \think\response\View
     */
    public function gonggao_list()
    {     //系统公告-列表
        $type = 'gonggao';
        $lists = db('wenzhang')->where('type', $type)->order('time desc')->field('id,title,titleb,fengmian,time,type,typeb,zuozhe,is_pop')->paginate(10);
        $this->assign('type', $type);
        $this->assign('type_cn', '系统公告');
        $this->assign('lists', $lists);
        return view('wenzhang_list');
    }

    /**
     * @name
     * @author zay
     * @Date date
     * @return \think\response\View
     */
    public function zhidao_list()
    {     //项目指导-列表
        $type = 'zhidao';
        $lists = db('wenzhang')->where('type', $type)->order('time desc')->field('id,title,titleb,fengmian,time,type,typeb,zuozhe,is_pop')->paginate(10);
        $this->assign('type', $type);
        $this->assign('type_cn', '项目指导');
        $this->assign('lists', $lists);
        return view('wenzhang_list');
    }

    public function wenzhang_add()
    {     //文章-添加
        $request = Request::instance();
        $type = $request->param()['type']; //文章类型
        $type_cn = [
            'gonggao' => '系统公告',
            'zhidao' => '项目指导',
        ];
        $type_cn = $type_cn[$type];

        if (!$request->isPost()) {
            $this->assign('type', $type);
            $this->assign('type_cn', $type_cn);
            return view();
        } else {
            $data = $request->param();
            //封面图上传
            $file = request()->file('fengmian');
            if ($file) {

                $fengmian = str_replace("\\", "/", $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif,bmp,jpeg,ico'])
                    ->move(ROOT_PATH . 'public' . DS . 'image' . DS . 'wenzhang')
                    ->getSaveName());
                $image = \think\Image::open('image/wenzhang/' . $fengmian);
                $image->thumb(150, 150, \think\Image::THUMB_CENTER)->save('image/wenzhang/' . $fengmian);
                $fengmian = '/image/wenzhang/' . $fengmian;
            }
            if (isset($data['is_pop']) && $data['is_pop'] == 1) {
                DB('wenzhang')->where(['is_pop' => 1])->update(['is_pop' => 0]);
            }
            $ret = db('wenzhang')->insertGetId([
                'title' => $data['title'],
                'titleb' => $data['titleb'],
                'fengmian' => isset($fengmian) ? $fengmian : '',
                'time' => time(),
                'type' => $data['type'],
                'typeb' => $data['typeb'],
                'zuozhe' => $data['zuozhe'],
                'text' => $data['editorValue'],
                'is_pop' => isset($data['is_pop']) ? $data['is_pop'] : 0,
            ]);
            if ($ret) {
                $this->success('发布成功', $type . '_list');
            } else {
                $this->error('发布失败', $type . '_list');
            }
        }
    }

    /**
     * @name 编辑公告
     * @author zay
     * @Date 2020/3/19 0019 16:16
     * @return \think\response\View
     */
    public function wenzhang_edit()
    {     //文章-编辑
        $request = Request::instance();
        $type = $request->param()['type']; //文章类型
        $id = $request->param()['id']; //文章id
        $type_cn = [
            'gonggao' => '系统公告',
            'zhidao' => '项目指导',
        ];
        $type_cn = $type_cn[$type];

        if (!$request->isPost()) {
            $list = db('wenzhang')->where('id', $id)->find();
            $this->assign('list', $list);
            $this->assign('type', $type);
            $this->assign('type_cn', $type_cn);
            return view();
        } else {
            $data = $request->param();
            //封面图上传
            $file = request()->file('fengmian');
            if ($file) {
                $fengmian = str_replace("\\", "/", $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif,bmp,jpeg,ico'])
                    ->move(ROOT_PATH . 'public' . DS . 'image' . DS . 'wenzhang')
                    ->getSaveName());
                $image = \think\Image::open('image/wenzhang/' . $fengmian);
                $image->thumb(150, 150, \think\Image::THUMB_CENTER)->save('image/wenzhang/' . $fengmian);
                $fengmian = '/image/wenzhang/' . $fengmian;
            }

            $update['title'] = $data['title'];
            $update['titleb'] = $data['titleb'];
            $update['time'] = time();
            $update['type'] = $data['type'];
            $update['typeb'] = $data['typeb'];
            $update['zuozhe'] = $data['zuozhe'];
            $update['text'] = $data['editorValue'];
            $update['is_pop'] = isset($data['is_pop']) ? $data['is_pop'] : 0;
            if ($update['is_pop'] == 1) {
                DB('wenzhang')->where(['is_pop' => 1])->update(['is_pop' => 0]);
            }
            if (isset($fengmian)) {
                $update['fengmian'] = $fengmian;
            }
            $ret = db('wenzhang')->where('id', $id)->update($update);
            if ($ret) {
                $this->success('修改成功', $type . '_list');
            } else {
                $this->error('修改失败', $type . '_list');
            }
        }
    }

    public function wenzhang_del()
    {     //文章-删除
        $request = Request::instance();
        if ($request->isGet() && isset($request->param()['id'])) {
            $id = $request->param()['id'];
            $ret = db('wenzhang')->where('id', $id)->delete();
            if ($ret) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('非法访问');
        }
    }
}
